package com.itshidu.jiaowu.controller;


import com.itshidu.jiaowu.dao.UserMapper;
import com.itshidu.jiaowu.dao.ViolateMapper;
import com.itshidu.jiaowu.entity.User;
import com.itshidu.jiaowu.entity.Violate;
import com.itshidu.jiaowu.service.ViolateService;
import com.itshidu.jiaowu.util.CookieUtils;
import com.itshidu.jiaowu.util.Result;
import com.itshidu.jiaowu.vo.ViolateVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author XiaZengming123
 * @since 2019-07-18
 */
@Controller
public class ViolateAction {

    @Autowired
    ViolateService ViolateService;
    @Autowired
    ViolateMapper violateMapper;
    @Autowired
    UserMapper userMapper;

    //新增处分记录
    @ResponseBody
    @RequestMapping("/admin/violate/add")
    public Result AddViolate(String name, Integer deduct_marks, String reason) {

        return ViolateService.save(name, deduct_marks, reason);
    }

    //查询所有记录
    @ResponseBody
    @RequestMapping("/admin/violate/list")
    public Result SelectViolate(int page, int limit) {
        return ViolateService.list(page, limit);
    }

    //删除记录
    @ResponseBody
    @RequestMapping("/admin/violate/del")
    public void DelViolate(Long id) {
        ViolateService.removeById(id);
    }

    //处分记录 点击编辑按钮 存储cookie信息
    @ResponseBody
    @RequestMapping("/admin/cookieInfo/edit")
    public Result EditViolate(Long id,HttpServletResponse response) {

        CookieUtils.writeCookie(response, "info", id.toString());
        return null;
    }

    //处分记录 点击编辑按钮后跳转修改页面
    @ResponseBody
    @RequestMapping("/eidt")
    public Object DelViolate(ModelAndView modelAndView, HttpServletRequest request,HttpServletResponse response) {

        String ids = CookieUtils.getCookie(request, "info");
        assert ids != null;
        Long id = Long.valueOf(ids);
        Violate violate = violateMapper.selectById(id);
        User teacher = userMapper.selectById(violate.getHandlerId());
        User student = userMapper.selectById(violate.getStudentId());
        ViolateVo violateVo = new ViolateVo();
        violateVo.setId(id);
        violateVo.setHandler(teacher.getUsername());
        violateVo.setStudent(student.getUsername());
        violateVo.setScore(violate.getScore());
        violateVo.setReason(violate.getReason());
        violateVo.setScore(violate.getScore());
        modelAndView.addObject("vio", violateVo);
        modelAndView.setViewName("violate_edit");
        return modelAndView;
    }

    //处分记录修改
    @ResponseBody
    @RequestMapping("/admin/violateInfo/edit")
    public Result EditViolate(Long ID,String handler,String name,Integer deduct_marks,String reason) {

        return ViolateService.EditViolate(ID,handler,name,deduct_marks,reason);
    }


    //搜索功能
    @ResponseBody
    @RequestMapping("/admin/violate/list1")
    public  Result  selectByname(Integer page,Integer limit,String studentName){
        Result r=ViolateService.sorl(page,limit,studentName);
        return  r;
    }


}

